<?php

include 'templateSys/SQL.php';
include 'templateSys/Settings.php';
include 'templateSys/manager_util.php';
include 'templateSys/Admin.php';


//loginCheck
checkUserFinal();

global $link;
$link = getLink();
$thisName = "editTypeResolution";

//vymazani itemu
if (isset($_POST["delteItemConfirmYes"]) || isset($_POST["delteItemConfirmNo"])) {
    if (isset($_POST["delteItemConfirmNo"])) {
        getUser()->removeProperty($thisName . "itemId");
        getUser()->removeProperty($thisName . "typeId");
        header("location: " . Settings::$rootPath . "" . Settings::$manager . "?action=" . getUser()->removeProperty($thisName . "page"));
    } elseif (isset($_POST["delteItemConfirmYes"])) {
        $page = getUser()->removeProperty($thisName . "page");
        $itemId = getUser()->removeProperty($thisName . "itemId");
        $typeId = getUser()->removeProperty($thisName . "typeId");
        $query = sprintf(SQL::$delete_itemFromMap_byItemTypeId_date, mysql_real_escape_string($itemId), mysql_real_escape_string($typeId)
        );
        $result = mq($query, $link);
        $query = sprintf(SQL::$delete_itemFromMap_byItemTypeId_text, mysql_real_escape_string($itemId), mysql_real_escape_string($typeId)
        );
        $result = mq($query, $link);
        $query = sprintf(SQL::$delete_itemFromMap_byItemTypeId_int, mysql_real_escape_string($itemId), mysql_real_escape_string($typeId)
        );
        $result = mq($query, $link);
        header("location: " . Settings::$rootPath . "" . Settings::$manager . "?action=" . $page);
    }
    saveSession();
    mysql_close($link);
    exit();
}
//vymazani itemu confirm page
if (isset($_POST["delteItem"]) && isset($_POST["page"]) && isset($_POST["itemId"]) && isset($_POST["typeId"])) {
    getUser()->setProperty($thisName . "page", $_POST["page"]);
    getUser()->setProperty($thisName . "itemId", $_POST["itemId"]);
    getUser()->setProperty($thisName . "typeId", $_POST["typeId"]);
    echo parseTemplate(loadTemplate("sysTemp/confirmAdminItemDelete"));
    saveSession();
    mysql_close($link);
    exit();
}
//opustení editace
if (isset($_POST["save"])) {
    getUser()->removeProperty("addingItem");
    getUser()->removeProperty("templateId");
    header("location: " . Settings::$rootPath . "" . Settings::$manager . "?action=" . getUser()->removeProperty("page"));
    saveSession();
    mysql_close($link);
    exit();
}
//editace caption
if (isset($_POST["templateName"]) && isset($_POST["page"]) && isset($_POST["captionEdit"]) && isset($_POST["templateValue"])) {
    getUser()->setProperty("templateValue", $_POST["templateValue"]);
    getUser()->setProperty("page", $_POST["page"]);
    saveSession();
    header("location: " . Settings::$rootPath . "editTextResolution.php");
    mysql_close($link);
    exit();
}
//vrace zpet k itemu
if (getUser()->issetProperty("addingItem") && getUser()->issetProperty("backFromEdit")) {
    global $itemTable;
    $query = sprintf(SQL::$select_itemMap_byTypeIdAndItemId, mysql_real_escape_string(getUser()->getPropertyValue("templateId")), mysql_real_escape_string(getUser()->getPropertyValue("addingItem"))
    );
    $result = mysql_query($query, $link);
    while ($row = mysql_fetch_assoc($result)) {
        $itemTable = $itemTable . "<tr>\n\t<td>\n\t" . $row["name"] . "\n\t</td>\n\t<td>\n\t" . getValue($row["valueId"], $row["valueType"]) . "\n\t</td>\n\t<td>\n\t" . getFrom($row["valueId"], $row["valueType"], getUser()->getPropertyValue("page")) . "\n\t</td>\n</tr>\n";
    }
    echo parseTemplate(loadTemplate("editItem"));
    getUser()->removeProperty("backFromEdit");
    saveSession();
    mysql_close($link);
    exit();
}
//zacatek addItem
if (isset($_POST["templateName"]) && isset($_POST["page"]) && isset($_POST["addItem"])) {
    global $itemTable;
    $query = sprintf(SQL::$select_itemMap_byType, mysql_real_escape_string($_POST["templateName"])
    );
    $result = mysql_query($query, $link);
    $genInt = generateInt();
    while ($row = mysql_fetch_assoc($result)) {
        $valueType = $row["valueType"];
        $gen = generateName();
        $templateId = $row["id"];
        createEmptyItemFull($row["name"], $templateId, $gen, $valueType, $genInt);
        if ("TEXT" == Settings::$type[$valueType]) {
            createEmptyText($gen);
        }
        if ("INT" == Settings::$type[$valueType]) {
            createEmptyInt($gen);
        }
        if ("DATE" == Settings::$type[$valueType]) {
            createEmptyDate($gen);
        }
        $itemTable = $itemTable . "<tr>\n\t<td>\n\t" . $row["name"] . "\n\t</td>\n\t<td>\n\t" . "" . "\n\t</td>\n\t<td>\n\t" . getFrom($gen, $valueType, $_POST["page"]) . "\n\t</td>\n</tr>\n";
    }
    echo parseTemplate(loadTemplate("editItem"));
    getUser()->setProperty("addingItem", $genInt);
    getUser()->setProperty("templateId", $templateId);
    getUser()->setProperty("page", $_POST["page"]);
    saveSession();
    mysql_close($link);
    exit();
}

function getFrom($name, $valueType, $page) {
    return '<form method = "post" action = "' . Settings::$typeResolutionArray[$valueType] . '"><input type="hidden" name = "page" value = "' . $page . '"><input type="hidden" name = "name" value = "' . $name . '"><input type="submit" name="submit" value = "EDIT"/></form>';
}

?>
